<template>
  <div>
    <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto"
      class="demo-ruleForm" status-icon>
      <el-form-item label="父级菜单" prop="parentId">
        <el-select v-model="ruleForm.parentId" placeholder="父级菜单">
          <el-option v-for="item in MenuModel" :key="item.menuId" :label="item.menuName" :value="item.menuId" />
        </el-select>
      </el-form-item>
      <el-form-item label="菜单名称" prop="menuName" @change="OneMenuName">
        <el-input v-model="ruleForm.menuName" />
      </el-form-item>
      <el-form-item label="跳转的Url地址" prop="url">
        <el-input v-model="ruleForm.url" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          添加菜单
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus';
import moment from 'moment';
import { compile, computed, onMounted, reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router';
import { useCounterStore } from '@/stores/counter';
import DepData from '@/stores/departmentData'
import axios from 'axios';
//pini保存
const store = useCounterStore();
//跳转
const router = useRouter();
//传参
const route = useRoute();



//唯一状态
const OneNameStatu = reactive({
  status: ''
}) //"1" 名称重复 "2" 名称不重复

// const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()

//仓储对象
const ruleForm = reactive<RuleForm>({
  "menuName": "",
  "url": "",
  "parentId": '',
  "createName": store.ccstore.nickName,
})

const locationOptions = ['Home', 'Company', 'School']

const rules = reactive({
  menuName: [
    { required: true, message: '菜单名称不可为空', trigger: 'blur' },
  ],
  url: [
    { required: true, message: '地址不可为空', trigger: 'blur' },
  ],
});


//唯一验证
const OneMenuName = () => {
  axios.get(`https://localhost:7174/api/Menu/OnenessMenuName?ParentId=${ruleForm.parentId}&MenuName=${ruleForm.menuName}`).then(res => {
    OneNameStatu.status = res.data.data
    console.log(OneNameStatu.status, '状态')
  })
}

//添加
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log(OneNameStatu.status, '唯一状态')
      if (OneNameStatu.status == "1") {
        ElMessage.error("同一级下,菜单名称重复")
        return;
      }
      axios.post("https://localhost:7272/api/Menu/AddMenu", ruleForm).then(res => {
        if (res.data.code == 212) {
          ElMessage.success(res.data.msg);
          router.push("/MenulistView");
        }
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}



//#region  下拉框父级数据
//父级仓储
const MenuModel = ref([])
const getparent = () => {
  axios.get("https://localhost:7174/api/Menu/GetMenuName", {}).then(res => {
    MenuModel.value = res.data.data
  })
}
onMounted(() => {
  getparent();
})

//#endregion


</script>

<style scoped></style>